<?php

namespace App\Http\Controllers\yefm;

//use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Book;
use App\Models\Bags;
use App\Models\BagsLiuyan;
use Request;
use Session;
use Redirect;

class YefmController extends Controller
{

    public function jssdk_sig()
    {
        //喜马拉雅获 - 签名接口

        //获得所有的请求头
        // $headers = apache_request_headers();

        //1、接收 params 参数
        // $params = $_POST['params'];
        $params = request::input('params');

        //JSON 格式的字符串进行解码，并转换为 PHP 变量
        $params = json_decode($params, false);
        // return $params->client_id;

        //2、将解析得到的参数原始值按照参数名的字典序排序

        //3、将排序后的参数键值对用&拼接
        $urlkey = 'client_id='. $params->client_id .'&device_id='. $params->device_id .'&grant_type='. $params->grant_type .'&nonce='. $params->nonce .'&timestamp='. $params->timestamp;

        //4、字符串进行Base64编码
        $base64EncodedStr = base64_encode($urlkey);

        //5、用 app_secret 作为 HMAC-SHA1 哈希 key（即sha1Key）
        $app_secret = 'c556de0528447fd178469c7032958194';
        $sha1Key = $app_secret;

        //使用sha1Key对base64EncodedStr进行HMAC-SHA1哈希得到字节数组
        $sha1ResultBytes = hash_hmac('sha1', $base64EncodedStr, $sha1Key, true);

        //对sha1ResultBytes进行MD5得到32位字符串，即为sig
        $sig = md5($sha1ResultBytes);

        // $arr_data = array('dd' => $base64EncodedStr);
        // $arr_data2 = array(
        //                     'client_id' => $params->client_id, 
        //                     'device_id' => $params->device_id,
        //                     'grant_type' => $params->grant_type,
        //                     'nonce'     => $params->nonce,
        //                     'timestamp' => $params->timestamp,                            
        //                     'sig'        => $sig
        //                 );
        $arr_data = array('code'=>0, 'message'=>'success', 'signature'=>$sig);

        //数组转换为 JSON 格式数据
        $json_data = json_encode($arr_data);

        return $json_data;
    }

    public function yefm()
    {
        //首页
        return view('yefm.index');
    }

    public function category()
    {
        //喜马拉雅全部分类
        return view('yefm.category');
    }

    public function albums_browse($album_id)
    {
        //专辑浏览 /albums/browse
        return view('yefm.albums_browse',array(
            'album_id'  => $album_id
        ));
    }

    public function tracks($album_id,$tracks_id)
    {
        //收听声音
        return view('yefm.tracks',array(
            'album_id' => $album_id, //专辑id
            'tracks_id' => $tracks_id //声音id
        ));
    }

    public function category_list($id)
    {
        //对应对应分类里专辑里列表
        return view('yefm.category_list', array('id'=>$id));
    }

    public function search()
    {
        //搜索页面
        return view('yefm.search');
    }

    public function searchkey()
    {
        //搜索内容显示
        $search_key = request::input('search_key');
        return view('yefm.searchkey',array('search_key' => $search_key));
    }

    public function search_album($search_key)
    {
        //搜索专辑内容
        return view('yefm.search_album',array('search_key' => $search_key));
    }

    public function search_sound($search_key)
    {
        //搜索声音内容
        return view('yefm.search_sound',array('search_key' => $search_key));
    }

    


    public function test()
    {
        //喜马拉雅代码测试
        return view('yefm.test');
    }

    public function test1()
    {
        //喜马拉雅代码测试
        return view('yefm.test1');
    }
        
}
